

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="ShanCe">
  <meta name="keywords" content="">
  
    <meta name="description" content="Docker 与 虚拟机的区别虚拟机：虚拟机模拟了完整的硬件系统，包括处理器、内存、存储、网络等。每个虚拟机拥有自己的操作系统和应用程序，并相互隔离。 Docker: 直接运行在宿主的操作系统上，共享同一个内核，由于容器不需要独立的操作系统，所以比虚拟机更轻量级，启动快，占用资源少。 安装docker123wget -O &#x2F;etc&#x2F;yum.repos.d&#x2F;docker-ce.repo https:">
<meta property="og:type" content="article">
<meta property="og:title" content="docker基础了解">
<meta property="og:url" content="https://sweetshance.github.io/2025/02/20/docker/docker%E5%9F%BA%E7%A1%80%E4%BA%86%E8%A7%A3/index.html">
<meta property="og:site_name" content="ShanCe的博客">
<meta property="og:description" content="Docker 与 虚拟机的区别虚拟机：虚拟机模拟了完整的硬件系统，包括处理器、内存、存储、网络等。每个虚拟机拥有自己的操作系统和应用程序，并相互隔离。 Docker: 直接运行在宿主的操作系统上，共享同一个内核，由于容器不需要独立的操作系统，所以比虚拟机更轻量级，启动快，占用资源少。 安装docker123wget -O &#x2F;etc&#x2F;yum.repos.d&#x2F;docker-ce.repo https:">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2025-02-20T02:51:46.000Z">
<meta property="article:modified_time" content="2025-05-23T10:19:34.970Z">
<meta property="article:author" content="ShanCe">
<meta property="article:tag" content="docker 概述">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>docker基础了解 - ShanCe的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"sweetshance.github.io","root":"/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>ShanCe</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="docker基础了解"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        ShanCe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2025-02-20 10:51" pubdate>
          2025年2月20日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          4.1k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          35 分钟
        
      </span>
    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">docker基础了解</h1>
            
            
              <div class="markdown-body">
                
                <h3 id="Docker-与-虚拟机的区别"><a href="#Docker-与-虚拟机的区别" class="headerlink" title="Docker 与 虚拟机的区别"></a>Docker 与 虚拟机的区别</h3><p>虚拟机：虚拟机模拟了完整的硬件系统，包括处理器、内存、存储、网络等。每个虚拟机拥有自己的操作系统和应用程序，并相互隔离。</p>
<p>Docker: 直接运行在宿主的操作系统上，共享同一个内核，由于容器不需要独立的操作系统，所以比虚拟机更轻量级，启动快，占用资源少。</p>
<h3 id="安装docker"><a href="#安装docker" class="headerlink" title="安装docker"></a>安装docker</h3><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs awk">wget -O <span class="hljs-regexp">/etc/yum</span>.repos.d<span class="hljs-regexp">/docker-ce.repo https:/</span><span class="hljs-regexp">/mirrors.ustc.edu.cn/</span>docker-ce<span class="hljs-regexp">/linux/</span>centos/docker-ce.repo<br>sed -i <span class="hljs-string">&#x27;s#download.docker.com#mirrors.ustc.edu.cn/docker-ce#g&#x27;</span> <span class="hljs-regexp">/etc/yum</span>.repos.d/docker-ce.repo<br>yum install docker-ce -y<br></code></pre></td></tr></table></figure>

<h3 id="修改docker配置"><a href="#修改docker配置" class="headerlink" title="修改docker配置"></a>修改docker配置</h3><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs awk"><span class="hljs-comment"># 修改启动文件，监听远程端口</span><br>vim <span class="hljs-regexp">/usr/</span>lib<span class="hljs-regexp">/systemd/</span>system/docker.service<br>ExecStart\=<span class="hljs-regexp">/usr/</span>bin<span class="hljs-regexp">/dockerd -H unix:/</span><span class="hljs-regexp">//</span>var<span class="hljs-regexp">/run/</span>docker.sock -H tcp:<span class="hljs-regexp">//</span><span class="hljs-number">10.0</span>.<span class="hljs-number">0.100</span>:<span class="hljs-number">2375</span> <br>systemctl daemon\-reload<br>systemctl enable docker.service <br>systemctl restart docker.service <br><span class="hljs-comment"># ps -ef检查进行，是否启动</span><br></code></pre></td></tr></table></figure>
<h3 id="Docker基础命令"><a href="#Docker基础命令" class="headerlink" title="Docker基础命令"></a>Docker基础命令</h3><h4 id="查看docker相关信息"><a href="#查看docker相关信息" class="headerlink" title="查看docker相关信息"></a>查看docker相关信息</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs bash">shance@shance:~$ docker version<br>Client: Docker Engine - Community<br> Version:           27.4.0-rc.2<br> API version:       1.40 (downgraded from 1.47)<br> Go version:        go1.22.9<br> Git commit:        af56ef5<br> Built:             Tue Nov 19 13:38:48 2024<br> OS/Arch:           linux/amd64<br> Context:           default<br><br>Server: Docker Engine - Community<br> Engine:<br>  Version:          19.03.9<br>  API version:      1.40 (minimum version 1.12)<br>  Go version:       go1.13.10<br>  Git commit:       9d988398e7<br>  Built:            Fri May 15 00:23:53 2020<br>  OS/Arch:          linux/amd64<br>  Experimental:     <span class="hljs-literal">false</span><br> containerd:<br>  Version:          1.7.23<br>  GitCommit:        57f17b0a6295a39009d861b89e3b3b87b005ca27<br> runc:<br>  Version:          1.1.14<br>  GitCommit:        v1.1.14-0-g2c9f560<br> docker-init:<br>  Version:          0.18.0<br>  GitCommit:        fec3683<br></code></pre></td></tr></table></figure>

<h4 id="配置docker-镜像加速"><a href="#配置docker-镜像加速" class="headerlink" title="配置docker 镜像加速"></a>配置docker 镜像加速</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs bash">vi /etc/docker/daemon.json<br>&#123; <span class="hljs-string">&quot;registry-mirrors&quot;</span>: [<span class="hljs-string">&quot;https://registry.docker-cn.com&quot;</span>]&#125;<br></code></pre></td></tr></table></figure>

<h4 id="启动第一个容器"><a href="#启动第一个容器" class="headerlink" title="启动第一个容器"></a>启动第一个容器</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker run -d -p 80:80 --name nginx nginx:latest<br></code></pre></td></tr></table></figure>
<p>参数说明<br>run: 运行容器<br>-d: 后台运行容器，并打印容器ID。<br>-p 80:80: 将容器的80端口映射到主机的80端口，这样，在主机的浏览器中访问80端口，即可访问到容器中的Nginx服务。<br>nginx:  镜像名称</p>
<h4 id="搜索官方镜像"><a href="#搜索官方镜像" class="headerlink" title="搜索官方镜像"></a>搜索官方镜像</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker search nginx<br></code></pre></td></tr></table></figure>

<h4 id="获取镜像"><a href="#获取镜像" class="headerlink" title="获取镜像"></a>获取镜像</h4><ol>
<li>根据镜像名拉取镜像</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker pull nginx:latest<br></code></pre></td></tr></table></figure>

<ol start="2">
<li><p>查看当前主机镜像列表</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker image list<br><span class="hljs-comment"># 或者</span><br>docker images<br></code></pre></td></tr></table></figure></li>
<li><p>拉取第三方镜像方法</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker pull registry.cn-hangzhou.aliyuncs.com/library/nginx:latest<br></code></pre></td></tr></table></figure></li>
</ol>
<h4 id="导出镜像"><a href="#导出镜像" class="headerlink" title="导出镜像"></a>导出镜像</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker image save centos &gt; docker-centos.tar.gz<br></code></pre></td></tr></table></figure>
<p>注意：</p>
<ol>
<li>导出发送给别人可以用来恢复</li>
<li>导出的镜像不包含在容器中下载的内容   </li>
</ol>
<h4 id="导入镜像"><a href="#导入镜像" class="headerlink" title="导入镜像"></a>导入镜像</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker image load &lt; docker-centos.tar.gz<br><br>docker images<br></code></pre></td></tr></table></figure>

<h4 id="查看镜像详细信息"><a href="#查看镜像详细信息" class="headerlink" title="查看镜像详细信息"></a>查看镜像详细信息</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker image inspect centos<br></code></pre></td></tr></table></figure>

<h4 id="删除镜像"><a href="#删除镜像" class="headerlink" title="删除镜像"></a>删除镜像</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs bash"><span class="hljs-comment"># 删除镜像</span><br>docker rmi centos  <br><br><span class="hljs-comment"># 强制删除镜像(用于相应容器正在运行的)</span><br>docker rmi -f centos<br><br><span class="hljs-comment"># 删除未使用的镜像</span><br>docker image prune -a <br><br><br></code></pre></td></tr></table></figure>

<h3 id="容器的日常管理"><a href="#容器的日常管理" class="headerlink" title="容器的日常管理"></a>容器的日常管理</h3><ol>
<li>运行容器<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs bash"><span class="hljs-comment"># 第一次运行</span><br>docker run -d -p 80:80 --name nginx nginx:latest<br><span class="hljs-comment"># 第二次运行</span><br>docker start nginx(容器名或者容器<span class="hljs-built_in">id</span>)<br></code></pre></td></tr></table></figure></li>
<li>查看容器详细信息<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker container inspect nginx(容器名或者容器<span class="hljs-built_in">id</span>)<br></code></pre></td></tr></table></figure></li>
<li>停止容器<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs bash">dcoker stop nginx(容器名或者容器<span class="hljs-built_in">id</span>)<br><span class="hljs-comment"># 或者</span><br>docker container <span class="hljs-built_in">kill</span> nginx(容器名或者容器<span class="hljs-built_in">id</span>)<br></code></pre></td></tr></table></figure></li>
<li>进入容器<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker <span class="hljs-built_in">exec</span> -it nginx bash<br><span class="hljs-comment"># 或者</span><br>docker run -it nginx bash<br></code></pre></td></tr></table></figure></li>
<li>退出容器<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs bash"><span class="hljs-built_in">exit</span> <br>ctrl+p <br>ctrl+q<br></code></pre></td></tr></table></figure></li>
<li>删除容器<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs bash"><span class="hljs-comment"># 删除单个</span><br>docker <span class="hljs-built_in">rm</span> nginx(容器名或者容器<span class="hljs-built_in">id</span>)<br><span class="hljs-comment"># 删除多个</span><br>docker <span class="hljs-built_in">rm</span> nginx1 nginx2<br><span class="hljs-comment"># 删除所有</span><br>docker <span class="hljs-built_in">rm</span> $(docker ps -a -q)<br><span class="hljs-comment"># 或者</span><br>docker <span class="hljs-built_in">rm</span> -f $(docker ps -a -q)<br></code></pre></td></tr></table></figure></li>
<li>启动时进行端口映射<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker run -d -p 80:80 --name nginx nginx:latest<br></code></pre></td></tr></table></figure></li>
</ol>
<h3 id="Docker数据卷的管理"><a href="#Docker数据卷的管理" class="headerlink" title="Docker数据卷的管理"></a>Docker数据卷的管理</h3><h4 id="挂载数据卷"><a href="#挂载数据卷" class="headerlink" title="挂载数据卷"></a>挂载数据卷</h4><ol>
<li>挂载数据卷<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker run -d -p 80:80 --name nginx -v /data/nginx:/usr/share/nginx/html nginx:latest<br></code></pre></td></tr></table></figure>
/usr/share/nginx/html 是容器中的目录<br>/data/nginx 是宿主机中的目录</li>
<li>设置共享卷,使用同一个卷启动一个新的容器<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker<br></code></pre></td></tr></table></figure></li>
<li>查看卷列表<br>docker volume ls</li>
</ol>
<h4 id="创建卷后挂载"><a href="#创建卷后挂载" class="headerlink" title="创建卷后挂载"></a>创建卷后挂载</h4><ol>
<li>创建卷<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker volume create my-vol<br><br>docker volume <span class="hljs-built_in">ls</span> <br></code></pre></td></tr></table></figure></li>
<li>查看卷路径<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker volume inspect my-vol<br></code></pre></td></tr></table></figure></li>
<li>使用卷创建容器<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker run -d -p 80:80 --name nginx -v my-vol:/usr/share/nginx/html nginx:latest<br></code></pre></td></tr></table></figure>
<h4 id="将容器保存为镜像"><a href="#将容器保存为镜像" class="headerlink" title="将容器保存为镜像"></a>将容器保存为镜像</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker commit nginx my-nginx(自己新的镜像名)<br></code></pre></td></tr></table></figure>
使用场景: 在镜像中添加某些环境或者程序打包成新的镜像 发布供使用</li>
</ol>
<h3 id="Dockerfile-构建docker镜像"><a href="#Dockerfile-构建docker镜像" class="headerlink" title="Dockerfile 构建docker镜像"></a>Dockerfile 构建docker镜像</h3><blockquote>
<p><a target="_blank" rel="noopener" href="https://github.com/CentOS/CentOS-Dockerfiles">https://github.com/CentOS/CentOS-Dockerfiles</a></p>
</blockquote>
<h4 id="dockers指令集"><a href="#dockers指令集" class="headerlink" title="dockers指令集"></a>dockers指令集</h4><ol>
<li>dockerfile 主要组成<br>基础镜像信息 FROM centos:6.8</li>
</ol>
<p>制作镜像操作指令RUN yum insatll openssh-server -y</p>
<p>容器启动时执行指令 CMD [“/bin/bash”]</p>
<ol start="2">
<li>dockerfile常用指令</li>
</ol>
<p>FROM 这个镜像的妈妈是谁？（指定基础镜像）</p>
<p>MAINTAINER 告诉别人，谁负责养它？（指定维护者信息，可以没有）</p>
<p>RUN 你想让它干啥（在命令前面加上RUN即可）</p>
<p>ADD 给它点创业资金（COPY文件，会自动解压）</p>
<p>WORKDIR 我是cd,今天刚化了妆（设置当前工作目录）</p>
<p>VOLUME 给它一个存放行李的地方（设置卷，挂载主机目录）</p>
<p>EXPOSE 它要打开的门是啥（指定对外的端口）</p>
<p>CMD 奔跑吧，兄弟！（指定容器启动后的要干的事情）</p>
<ol start="3">
<li>dockerfile其他指令：</li>
</ol>
<p>COPY 复制文件</p>
<p>ENV  环境变量</p>
<p>ENTRYPOINT  容器启动后执行的命令</p>
<h4 id="创建一个Dockerfile"><a href="#创建一个Dockerfile" class="headerlink" title="创建一个Dockerfile"></a>创建一个Dockerfile</h4><ol>
<li><p>书写dockerfile</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs bash"><span class="hljs-comment"># 创建目录</span><br>[root@docker01 base]<span class="hljs-comment"># cd /opt/base</span><br><span class="hljs-comment"># 创建Dcokerfile文件，注意大小写</span><br>[root@docker01 base]<span class="hljs-comment"># vim Dockerfile</span><br>FROM centos:6.8<br>RUN yum install openssh-server -y <br>RUN <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;root:123456&quot;</span> |chpasswd<br>RUN /etc/init.d/sshd start <br>CMD [<span class="hljs-string">&quot;/usr/sbin/sshd&quot;</span>,<span class="hljs-string">&quot;-D&quot;</span>]<br></code></pre></td></tr></table></figure></li>
<li><p>构建镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker build -t centos6.8:ssh .<br></code></pre></td></tr></table></figure></li>
<li><p>运行容器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker run -d -p 22:22 --name centos6.8 centos6.8:ssh<br></code></pre></td></tr></table></figure></li>
</ol>
<h4 id="使用Dcokerfile安装kodexplorer"><a href="#使用Dcokerfile安装kodexplorer" class="headerlink" title="使用Dcokerfile安装kodexplorer"></a>使用Dcokerfile安装kodexplorer</h4><figure class="highlight txt"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs txt">FROM centos:6.8<br>RUN yum install wget unzip php php-gd php-mbstring -y &amp;&amp; yum clean all<br># 设置工作目录，之后的操作都在这个目录中<br>WORKDIR /var/www/html/<br>RUN wget -c http://static.kodcloud.com/update/download/kodexplorer4.25.zip<br>RUN unzip kodexplorer4.25.zip &amp;&amp; rm -f kodexplorer4.25.zip<br>RUN chown -R apache.apache .<br>CMD [&quot;/usr/sbin/apachectl&quot;,&quot;-D&quot;,&quot;FOREGROUND&quot;]<br></code></pre></td></tr></table></figure>


<p>注意：<br>加速镜像源<br><a target="_blank" rel="noopener" href="https://docker.1ms.run/">https://docker.1ms.run</a><br><a target="_blank" rel="noopener" href="https://dockers.xuanyuan.me/">https://dockers.xuanyuan.me/</a></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/docker/" class="category-chain-item">docker</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/docker-%E6%A6%82%E8%BF%B0/">#docker 概述</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>docker基础了解</div>
      <div>https://sweetshance.github.io/2025/02/20/docker/docker基础了解/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>ShanCe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2025年2月20日</div>
        </div>
      
      
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2025/05/21/docker/docker%E9%83%A8%E7%BD%B2minio/" title="docker部署minio">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">docker部署minio</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2025/01/23/springBoot%E5%AD%A6%E4%B9%A0/mybatis%E5%A4%96%E9%83%A8%E6%8B%BC%E6%8E%A5sql/" title="mybatis外部拼接sql">
                        <span class="hidden-mobile">mybatis外部拼接sql</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="http://82.157.172.231/" target="_blank" rel="nofollow noopener"><span>无聊</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>ShanCe</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
